package znbkBkfx.BKFX_PPT;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.base.BaseRecyclerAdapter;
import com.example.znbk.znbklibrary.base.BaseRecyclerViewHolder;
import com.example.znbk.znbklibrary.util.LancooUtils;
import com.example.znbk.znbklibrary.widget.CustomTextViewToPicture;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import znbkBkfx.GetBkfxJson.BkfxPaperEntity;

public class BkfxPptAnswerAdapter extends BaseRecyclerAdapter {

    private Context context;
    private BkfxPaperEntity bkfxPaperEntity;
    private int childIndex = -1;
    private CallBackAdapterPpt callBackAdapterPpt;
    private LayoutInflater inflater;

    public BkfxPptAnswerAdapter(Context context, BkfxPaperEntity bkfxPaperEntity, int childIndex, CallBackAdapterPpt callBackAdapterPpt){
        this. context=context;
        this.bkfxPaperEntity = bkfxPaperEntity;
        this.childIndex = childIndex;
        this.callBackAdapterPpt = callBackAdapterPpt;
        inflater=LayoutInflater. from(context);
        getAnswerInfor();
    }

    private int subChildOptionCount = -1;
    private int SortIndex = -1;
    private int ChildListSize  =0;
    private void getAnswerInfor(){
        subChildOptionCount = bkfxPaperEntity.getQuesList().get(childIndex).getChildList().
                get(0).getSubChildList().get(0).getOptionList().size();
        SortIndex = bkfxPaperEntity.getQuesList().get(childIndex).getChildList().
                get(0).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();
        ChildListSize = bkfxPaperEntity.getPaperAnswer().get(childIndex).getChildList().
                get(0).getSubChildList().get(0).getSubChildItem().size();
    }

    @Override
    public int getItemCount() {
        return subChildOptionCount;
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    private TextView mTvChildQuesAsk;
    private LinearLayout mLlOption;
    private ScaleButton mSBtnOptionIndex;
    private TextView mTvOptionContent;

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder baseRecyclerViewHolder, final int position) {

        mLlOption = baseRecyclerViewHolder.getView(R.id.ll_option);

        Log.e("onBindViewHolder","====AAAA=============终于进来了====");
        mTvChildQuesAsk = baseRecyclerViewHolder.getView(R.id.tv_childQuesAsk);
        if(position==0){
            mTvChildQuesAsk.setVisibility(View.VISIBLE);
//            new CustomTextViewToPicture(context,mTvChildQuesAsk,SortIndex+"、 ");
            new CustomTextViewToPicture(context,mTvChildQuesAsk,"从下面选项中挑选合适的选项");
        }else{
            mTvChildQuesAsk.setVisibility(View.GONE);
        }


        mSBtnOptionIndex = baseRecyclerViewHolder.getView(R.id.sbtn_optionIndex);
        String OptionIndex = bkfxPaperEntity.getQuesList().get(childIndex).getChildList().
                get(0).getSubChildList().get(0).getOptionList().get(position).getOption();
        mSBtnOptionIndex.setText(OptionIndex);
        mSBtnOptionIndex.setTag(position);
        mSBtnOptionIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int SubmitState = bkfxPaperEntity.getPaperAnswer().get(childIndex).getSubmitState();
                if(SubmitState==2){
                    return;
                }

                String StuAnswer = LancooUtils.asciiToString(String.valueOf(65 + position));
                Log.e("mTvOptionContent","===============StuAnswer=====111111=========="+StuAnswer);

                callBackAdapterPpt.callClickWhichIndex(StuAnswer);
                doRefresh(position);

                int size = bkfxPaperEntity.getPaperAnswer().get(childIndex).getChildList().get(0).getSubChildList().get(0).getSubChildItem().size();
                for(int i=0;i<size;i++){
                    String AAA = bkfxPaperEntity.getPaperAnswer().get(childIndex).getChildList().get(0).getSubChildList().get(0).getSubChildItem().get(i).getStuAnswer();
                    Log.e("mTvOptionContent",i+"========111111=======答案=========="+AAA);
                }
            }
        });

        mTvOptionContent = baseRecyclerViewHolder.getView(R.id.tv_optionContent);
        String OptionContent = bkfxPaperEntity.getQuesList().get(childIndex).getChildList().
                get(0).getSubChildList().get(0).getOptionList().get(position).getContent();
        mTvOptionContent.setText(OptionContent);
        mTvOptionContent.setTag(position);
        mTvOptionContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int SubmitState = bkfxPaperEntity.getPaperAnswer().get(childIndex).getSubmitState();
                if(SubmitState==2){
                    return;
                }

                String StuAnswer = LancooUtils.asciiToString(String.valueOf(65 + position));
                callBackAdapterPpt.callClickWhichIndex(StuAnswer);
                doRefresh(position);
            }
        });

        int SubmitState = bkfxPaperEntity.getPaperAnswer().get(childIndex).getSubmitState();
        if(SubmitState==2){
            mLlOption.removeAllViews();
        }else {
            if (position == subChildOptionCount - 1) {
                mLlOption.removeAllViews();
                View mView = new View(context);
                mView.setBackgroundColor(Color.parseColor("#ffffff"));
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300);
                mLlOption.addView(mView, lp);
            }
        }

    }

    @Override
    public View getHolderView(ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.bkfx_adapter_item_answer_ppt,parent, false);
        return view;
    }

    public void doRefresh(int position){
        //输入学生作答
//        if(position!=-1) {
//            String currentStuAnswer = LancooUtils.asciiToString(String.valueOf(65 + position));
//            callBackAdapterChpp.callSetCurrentStuAnswer(currentStuAnswer);
//        }

        int AnsweredCount=0;
        int AnsweredCountTrue=0;
        Log.e("==AXC===","==================小题个数=====wwww=========="+ChildListSize);
        for(int i=0;i<ChildListSize;i++){
            String StuAnswer =  bkfxPaperEntity.getPaperAnswer().get(childIndex).getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getStuAnswer();
            if(!StuAnswer.equals("")){
                AnsweredCount = AnsweredCount+1;
            }

            String ItemAnswer =  bkfxPaperEntity.getQuesList().get(childIndex).getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getItemAnswer();
            if(ItemAnswer.equals(StuAnswer)){
                AnsweredCountTrue = AnsweredCountTrue+1;
            }
            Log.e("==AXC===","==================完成个数=====wwww=========="+AnsweredCount);
        }
        //若作答完成则显示答案，记录作答状态，否则亦然
        if (AnsweredCount == ChildListSize) {
            Log.e("==choosepos===","==================语音刷新=====全部做完==========");
            bkfxPaperEntity.getPaperAnswer().get(childIndex).setSubmitState(2);
            callBackAdapterPpt.callHideRecordMiView();
//            callBackAdapterChpp.callShowMaskDialog(answerResult);
            double EachScore = 0;
            String QuesList_QuesID = bkfxPaperEntity.getQuesList().get(childIndex).getQuesID();
            int PaperQuesSize = bkfxPaperEntity.getPaperQues().size();
            for(int i=0;i<PaperQuesSize;i++){
                int PaperQues_QuesListSize = bkfxPaperEntity.getPaperQues().get(childIndex).getQuesList().size();
                for(int j=0;j<PaperQues_QuesListSize;j++){
                    String PaperQues_QuesID = bkfxPaperEntity.getPaperQues().get(childIndex).getQuesList().get(j).getQuesID();
                    if(QuesList_QuesID.equals(PaperQues_QuesID)){
                        EachScore = bkfxPaperEntity.getPaperQues().get(childIndex).getEachScore();
                        break;
                    }
                }
            }
            double ResultStuScore = AnsweredCountTrue*EachScore;
            callBackAdapterPpt.callShowResultStuScore(ResultStuScore);
        }else{
            bkfxPaperEntity.getPaperAnswer().get(childIndex).setSubmitState(0);
        }

    }

    public interface CallBackAdapterPpt {
        void callHideRecordMiView();
        void callClickWhichIndex(String stuAnswer);
        void callShowResultStuScore(double stuScore);
        void callSetCurrentStuAnswer(String currentStuAnswer);
    }


}